package cn.iocoder.yudao.module.agent.service.brokerageuser;

import cn.hutool.core.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.agent.controller.admin.brokerageuser.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokerageuser.AgentBrokerageUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.agent.convert.brokerageuser.AgentBrokerageUserConvert;
import cn.iocoder.yudao.module.agent.dal.mysql.brokerageuser.AgentBrokerageUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.agent.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 代理人分销用户 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class AgentBrokerageUserServiceImpl implements AgentBrokerageUserService {

    @Resource
    private AgentBrokerageUserMapper agentBrokerageUserMapper;

    @Override
    public Long createBrokerageUser(AgentBrokerageUserCreateReqVO createReqVO) {
        // 插入
        AgentBrokerageUserDO brokerageUser = AgentBrokerageUserConvert.INSTANCE.convert(createReqVO);
        agentBrokerageUserMapper.insert(brokerageUser);
        // 返回
        return brokerageUser.getId();
    }

    @Override
    public void updateBrokerageUser(AgentBrokerageUserUpdateReqVO updateReqVO) {
        // 校验存在
        validateBrokerageUserExists(updateReqVO.getId());
        // 更新
        AgentBrokerageUserDO updateObj = AgentBrokerageUserConvert.INSTANCE.convert(updateReqVO);
        agentBrokerageUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteBrokerageUser(Long id) {
        // 校验存在
        validateBrokerageUserExists(id);
        // 删除
        agentBrokerageUserMapper.deleteById(id);
    }

    private void validateBrokerageUserExists(Long id) {
        if (agentBrokerageUserMapper.selectById(id) == null) {
            throw exception(BROKERAGE_USER_NOT_EXISTS);
        }
    }

    @Override
    public AgentBrokerageUserDO getBrokerageUser(Long id) {
        return agentBrokerageUserMapper.selectById(id);
    }

    @Override
    public AgentBrokerageUserDO getBrokerageUserByUserId(Long userId) {
        return agentBrokerageUserMapper.selectOne(AgentBrokerageUserDO::getUserId, userId);
    }

    @Override
    public List<AgentBrokerageUserDO> getBrokerageUsersByAgentUserId(Long userId) {
        return agentBrokerageUserMapper.selectList(AgentBrokerageUserDO::getAgentUserId, userId);
    }

    @Override
    public List<AgentBrokerageUserDO> getBrokerageUserList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return agentBrokerageUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AgentBrokerageUserDO> getBrokerageUserPage(AgentBrokerageUserPageReqVO pageReqVO) {
        return agentBrokerageUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AgentBrokerageUserDO> getBrokerageUserList(AgentBrokerageUserExportReqVO exportReqVO) {
        return agentBrokerageUserMapper.selectList(exportReqVO);
    }

    @Override
    public Boolean hasBinded(Long agentUserId, Long brokerageUserId) {
        AgentBrokerageUserDO agentBrokerageUserDO = agentBrokerageUserMapper.selectOne(AgentBrokerageUserDO::getUserId, brokerageUserId);
        if(agentBrokerageUserDO != null)
        {
            if(agentBrokerageUserDO.getAgentUserId() == agentUserId){
                throw exception(BROKERAGE_ACCOUNT_HAS_BINDED_TARGET);
            }else{
                throw exception(BROKERAGE_ACCOUNT_HAS_BINDED_OTHER);
            }
        }

        return false;
    }

    @Override
    public Boolean hasBinded(Long userId){
        AgentBrokerageUserDO agentBrokerageUserDO = agentBrokerageUserMapper.selectOne(AgentBrokerageUserDO::getUserId, userId);
        return agentBrokerageUserDO != null;
    }

    @Override
    public Long count(Long agentUserId){
        return agentBrokerageUserMapper.selectCount(AgentBrokerageUserDO::getAgentUserId, agentUserId);
    }

    @Override
    public Integer updateOrderCount(Long userId, Integer count, Integer price){
        return agentBrokerageUserMapper.updateOrderCount(userId, count, price);
    }
}
